package com.funtonow.jqdk.fkcs.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.funtonow.jqdk.R;
import com.funtonow.jqdk.adapter.JhAdapter;
import com.funtonow.jqdk.base.BaseBean;
import com.funtonow.jqdk.base.BaseFragment;
import com.funtonow.jqdk.base.Constants;
import com.funtonow.jqdk.bean.JhjlBean;
import com.funtonow.jqdk.bean.UserInfoBean;
import com.funtonow.jqdk.utils.HttpUtil;
import com.funtonow.jqdk.utils.LogUtil;
import com.funtonow.jqdk.utils.SPUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import okhttp3.Call;

import static com.funtonow.jqdk.base.UrlUtils.HK_LIST;
import static com.funtonow.jqdk.base.UrlUtils.JH_RECORD;

/**
 * @author Jiajien
 * @date 2019/8/23
 */
public class HKRecordFragment extends BaseFragment {

    @BindView(R.id.rlv_jh)
    RecyclerView recyclerView;

    private JhAdapter jhAdapter;

    public static HKRecordFragment newInstance() {
        Bundle args = new Bundle();
        HKRecordFragment fragment = new HKRecordFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View getLayout(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_jh, null);
    }

    @Override
    public void onCreateFragment(@Nullable Bundle savedInstanceState) {
        initRecyclerView(recyclerView);
        getRecord();
    }

    private void initRecyclerView(RecyclerView recyclerView) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        //设置增加或删除条目的动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        jhAdapter = new JhAdapter(1);
        recyclerView.setAdapter(jhAdapter);
    }

    private void getRecord() {
        UserInfoBean userInfoBean = SPUtil.getUserInfo();
        Map<String, String> param = new HashMap<>();
        assert userInfoBean != null;
        param.put("appid", Constants.OWN_APP_ID);
        param.put("token", userInfoBean.getToken());
        param.put("openid", userInfoBean.getOpenid());
        param.put("a", "1");
        HttpUtil.getInstance().postDataAsynToNet(JH_RECORD, param, new HttpUtil.MyNetCall() {

            @Override
            public void success(Call call, String jsonString) throws IOException {
                BaseBean baseBean = JSON.parseObject(jsonString, BaseBean.class);
                if (baseBean.errnum >= 0) {
                    LogUtil.i(baseBean.data+" **************  ");
                    List<JhjlBean> jhjlBeans = JSON.parseArray(baseBean.data, JhjlBean.class);
                    jhAdapter.setNewData(jhjlBeans);
                }
            }

            @Override
            public void failed(Call call, IOException e) {

            }
        });

    }

}
